package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.JzdxIdentityConvert;
import com.engine4cloud.tdf.supervision.dto.create.JzdxIdentityCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxIdentityResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxIdentityUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxIdentityEntity;
import com.engine4cloud.tdf.supervision.mapper.JzdxIdentityMapper;
import com.engine4cloud.tdf.supervision.service.JzdxIdentityService;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * 矫正对象证件信息
 *
 * @author wenjie
 * @date 2024-04-03 14:46:19
 */
@Service
public class JzdxIdentityServiceImpl extends ServiceImpl<JzdxIdentityMapper, JzdxIdentityEntity> implements JzdxIdentityService {
    @Override
    public JzdxIdentityResultDTO find(String code) {
        return this.baseMapper.findByCode(code);
    }

    @Override
    public Boolean create(JzdxIdentityCreateDTO dto, String code) {
        return this.save(JzdxIdentityConvert.INSTANCE.create(dto, code, SecurityUtils.getUser().getTenantId()));
    }

    @Override
    public Boolean updateByCode(JzdxIdentityUpdateDTO dto, String code) {
        JzdxIdentityResultDTO result = this.find(code);
        if (Objects.isNull(result)) {
            return this.create(JzdxIdentityConvert.INSTANCE.updateCreate(dto), code);
        }

        dto.setId(result.getId());
        return this.updateById(JzdxIdentityConvert.INSTANCE.update(dto));
    }
}